package com.dofast.bt.mvp.presenter.my;

import com.dofast.bt.adapter.CollectStoreListAdapter;
import com.dofast.bt.base.BaseApplication;
import com.dofast.bt.base.BaseMvpPresenter;
import com.dofast.bt.bean.AttentionBean;
import com.dofast.bt.bean.GoodsBean;
import com.dofast.bt.mvp.model.IMainModel;
import com.dofast.bt.mvp.model.MainModel;
import com.dofast.bt.mvp.view.activity.basket.StoreDetailActivity;
import com.dofast.bt.mvp.view.activity.my.CollectStoreListView;
import com.dofast.bt.util.Helper;

import org.xutils.ex.DbException;

import java.util.ArrayList;
import java.util.List;

/**
 * username:Created by hsj on 2016/12/22 0022.
 * class：
 * 类描述：收藏的店铺
 */

public class CollectStoreListPresenter extends BaseMvpPresenter<CollectStoreListView> implements ICollectStoreListPresenter {
    private List<AttentionBean> list = null;
    private List<GoodsBean> mList = null;
    private CollectStoreListAdapter adapter = null;
    private IMainModel mainModel = null;
    private GoodsBean goodsBean = null;


    /**
     * isCollectWord：true  表示收藏的文章界面
     */
    private boolean isCollectWord = false;

    public CollectStoreListPresenter() {
        mainModel = new MainModel();
    }

    @Override
    public void initData() {
        checkViewAttach();
        isCollectWord = mvpView.isCollectWord();
        list = new ArrayList<AttentionBean>();
        mList = new ArrayList<GoodsBean>();
        adapter = new CollectStoreListAdapter(list);
        mvpView.initAdapter(adapter);
        if (isCollectWord) {
            getData();
            mvpView.setTitle("热门店铺");
        } else {
            getData();
        }


    }



    /**
     * 获取数据
     */
    private void getData() {
        try {
            List<AttentionBean> temp = mainModel.getCollectStores();
            if (temp != null) {
                mvpView.hideEmpty();
                list.clear();
                list.addAll(temp);
                adapter.notifyDataSetChanged();
            } else {
                mvpView.showEmpty();
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void itemClick(int position) {

            mvpView.showDetail(list.get(position).getShopId());


    }

    @Override
    public void delete(int positon) {
        try {

                if (mainModel.cancelAttentionShop(list.get(positon).getShopId())) {
                    initData();
                    Helper.showToast("删除成功！");
                }

        } catch (DbException e) {
            e.printStackTrace();
        }
    }

}
